package cn.com.suiyi.shixingshengxian.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/7/19.
 */
public abstract class BaseFragment extends Fragment{
    public static final String FRAGMENT_OUT_STATE = "outState";
    private static final String PARMS_TILTE = "title";
    private View view;

    public static Fragment newInstance(String title) {
        Bundle args = new Bundle();
        args.putString(PARMS_TILTE, title);
        Fragment fragment = new Fragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (savedInstanceState != null) {
            if (savedInstanceState.getBoolean(FRAGMENT_OUT_STATE)) {
                getFragmentManager().beginTransaction().show(this).commit();
            } else {
                getFragmentManager().beginTransaction().hide(this).commit();
            }
        }
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(getLayout(),null);
        ButterKnife.inject(this, view);
        init();
        return view;
    }
    public final void init() {
        initView();
        initData();
    }

    protected abstract void initData();

    protected abstract void initView();
    /**
     * 获取布局id
     *
     * @return
     */
    public abstract int getLayout();

    @Override
    public void onDestroy() {
        super.onDestroy();
        ButterKnife.reset(this);
    }
    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putBoolean(FRAGMENT_OUT_STATE, isVisible());
    }
}
